require('./Timeselect.styl');
const { Calendar, DatetimeField } = SaltUI;

class Timeselect extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            value: null,
            startDate: '',//日历保存时间
            endDate: '',//日历时间
        };
    }


    handleChange(value) {
        this.setState({
            value: value
        });
        this.changeDate(value);
    }
    //日期区间
    changeDate(value) {
        var regEx = new RegExp("\\-", "gi");
        var milliseconds = value.toString().replace(regEx, "/");
        var currentDate = new Date();
        currentDate.setTime(milliseconds);
        var iDay = currentDate.getDay();
        console.log(iDay)
        iDay = (iDay == 0) ? 7 : iDay;
        let startDate = this.dateAdd("d", 1 - iDay, currentDate);
        let endDate = this.dateAdd("d", (7 - iDay), currentDate);
        var strStart = startDate.getFullYear() + "-" + (startDate.getMonth() + 1) + "-" + startDate.getDate();
        var strEnd = endDate.getFullYear() + "-" + (endDate.getMonth() + 1) + "-" + endDate.getDate();
        this.setState({ startDate: strStart, endDate: strEnd })
    }
    dateAdd(strInterval, Number, date) {
        var dtTmp = date.toString();
        switch (strInterval) {
            case 's':
                return new Date(Date.parse(dtTmp) + (1000 * Number));
            case 'n':
                return new Date(Date.parse(dtTmp) + (60000 * Number));
            case 'h':
                return new Date(Date.parse(dtTmp) + (3600000 * Number));
            case 'd':
                return new Date(Date.parse(dtTmp) + (86400000 * Number));
            case 'w':
                return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number));
            case 'q':
                return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number * 3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
            case 'm':
                return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
            case 'y':
                return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
        }
    }
    render() {
        let t = this;
        return (
            <div className="timeselect">
                <DatetimeField label="日期" onSelect={t.handleChange.bind(t)} value={t.state.value} placeholder="年/月/日" columns={['年', '月', '日']} />
                <div className="t-SML25 t-SPB10 t-SPT10">日期区间：{this.state.startDate}至{this.state.endDate}</div>
            </div>
        );
    }

    componentWillMount() {
    }

    componentDidMount() {
    }

    componentWillReceiveProps(nextProps) {
    }

    shouldComponentUpdate(nextProps, nextState) {
        return true;
    }

    componentWillUpdate(nextProps, nextState) {
    }

    componentDidUpdate(prevProps, prevState) {
    }

    componentWillUnmount() {
    }
}

module.exports = Timeselect;
